package com.sinoframework.web.servlet.advice;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sinoframework.web.servlet.bean.CustomRuntimeException;
import com.sinoframework.web.servlet.bean.ResponseResult;

/**
 *	 仅支持带有 @ResponseBody 的方法
 * 
 * @author Administrator
 *
 */
@RestControllerAdvice // 注意哦，这里要加上需要扫描的包
public class ResponseBodyControllerAdvice implements ResponseBodyAdvice<Object> {
	private static final Logger log = LogManager.getLogger();
	
	public ResponseBodyControllerAdvice() {
    	log.info("启用【接口统一返回增强类 ResponseBodyControllerAdvice】");
	}
	
	
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> aClass) {
        
    	Type type = returnType.getGenericParameterType();
    	
    	if(type instanceof ParameterizedType) {
    		Type rtType = ((ParameterizedType)returnType.getGenericParameterType()).getRawType();
        	
        	if(rtType.equals(ResponseResult.class)) {
        		return false;
        	}
        	
        	if(rtType.equals(ResponseEntity.class)) {
        		return false;
        	}
    	}
    	
    	
    	//获取【类】上是否包含 @IgnoreResponseBodyWapper
    	if(returnType.getExecutable().getDeclaringClass().isAnnotationPresent(IgnoreResponseBodyWapper.class)) {
    		return false;
    	}
    	//获取【方法】上是否包含 @IgnoreResponseBodyWapper
    	if(returnType.getExecutable().isAnnotationPresent(IgnoreResponseBodyWapper.class)) {
    		return false;
    	}
    	
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object data, MethodParameter returnType, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest request, ServerHttpResponse response) {
    	
    	// String类型不能直接包装，所以要进行些特别的处理
        if (returnType.getGenericParameterType().equals(String.class)) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                // 将数据包装在ResultVO里后，再转换为json字符串响应给前端
                return objectMapper.writeValueAsString(new ResponseResult<>(data));
            } catch (JsonProcessingException e) {
                throw new CustomRuntimeException("返回String类型错误");
            }
        }
        // 将原本的数据包装在ResultVO里
        return new ResponseResult<>(data);
    }
}